const Koa = require('koa');

const app = new Koa();

app.use(async (ctx, next) => {    //ctx变量封装了request和response两个重要属性对象
    console.log(ctx.request.url);
    console.log("url");
    //console.log(`${ctx.request.method} ${ctx.request.url}`); // 打印URL
    await next(); // 调用下一个middleware

    console.log("url await");
});

app.use(async (ctx, next) => {
    console.log("time");
    const start = new Date().getTime(); // 当前时间
    await next(); // 调用下一个middleware
    const ms = new Date().getTime() - start; // 耗费时间
    //console.log(`Time: ${ms}ms`); // 打印耗费时间
    console.log("time await");
});

app.use(async (ctx, next) => {
    console.log("html");
    await next();
    ctx.response.type = 'text/html';
    ctx.response.body = '<h1>Hello, koa2!</h1>';
    ctx.response.body = '<h1>Hello, koa2!</h1>';
    ctx.response.body = '<h1>Hello, koa2!</h1>';
    console.log("html await");
});

app.listen(3000);
console.log('app started at port 3000...');